package com.hc.music.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hc.music.common.R;
import com.hc.music.dao.LevelMapper;
import com.hc.music.entity.Level;
import com.hc.music.service.LevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LevelServiceImpl extends ServiceImpl<LevelMapper, Level> implements LevelService {
    @Autowired
    private LevelMapper levelMapper;
    @Override
    public R insertLevel(Level level) {
        if(levelMapper.insert(level) > 0){
            return R.success("评价成功");
        }else {
            return R.error("评价失败");
        }
    }

    @Override
    public R selectSumAvg(Integer songListId) {
        QueryWrapper<Level> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("song_list_id",songListId);
        Long rankNum = levelMapper.selectCount(queryWrapper);
        return R.success(null, (rankNum <= 0) ? 0 : levelMapper.selectSum(songListId) / rankNum);
    }

    @Override
    public R selectUserLevel(Integer userId, Integer songListId) {
        Integer integer = levelMapper.selectUserLevel(userId, songListId);
        if(integer != null){
            return R.success(null,integer);
        }else {
            return R.error(null);
        }
    }
}
